package com.parkcar;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import HttpUtils.HttpCarRequest;
import Modle.CarBean;
import adapter.CarAdapter;
import ui.RefreshLayout;
import util.BaseActivity;
import util.BaseTools;
import util.Constrants;

/**
 * Created by Administrator on 2016/2/19.
 */
public class CarActivity extends BaseActivity{
    private ListView mListView;
    private TextView ero_title,ero_content,ero_click;
    private RelativeLayout ero_ll;
    private HttpCarRequest carRequest,delRequest;
    private List<CarBean> mList = new ArrayList<>();
    private RefreshLayout pull_ly;
    private CarAdapter mAdapter;
    private  int position;
    private boolean isDelete;
    @Override
    protected void setUpui(Bundle arg0) {

        setContentView(R.layout.activity_car_list);
        setTitle("车辆管理");
        setRightListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isDelete = !isDelete;
                if(isDelete){
                    setRight_text("取消");
                    mAdapter.setDelete(true);
                }else{
                    mAdapter.setDelete(false);
                }
            }
        });
        initView();
        getData();
    }
    private void initView(){
        mListView = (ListView) findViewById(R.id.mListView);
        ero_title = (TextView) findViewById(R.id.ero_title);
        ero_content = (TextView) findViewById(R.id.ero_content);
        ero_click = (TextView) findViewById(R.id.ero_click);
        ero_ll = (RelativeLayout) findViewById(R.id.ero_ll);
        pull_ly = (RefreshLayout) findViewById(R.id.pull_ly);
        ero_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //添加车辆
                showActivity(AddCarActivity.class,null,Constrants.ADD_CAR);
            }
        });
        pull_ly.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener(){
            @Override
            public void onRefresh() {
                getData();
            }
        });
         pull_ly.setLoading(false);
        mAdapter = new CarAdapter(this);
        mListView.addHeaderView(getLayoutInflater().inflate(R.layout.header_car,null));
        mListView.addFooterView(getLayoutInflater().inflate(R.layout.buttom_car,null));
        mListView.setAdapter(mAdapter);
    }
    @Override
    protected void updateUI(boolean isLogin) {

    }
    private void getData(){
        carRequest = new HttpCarRequest(this,1);
        carRequest.setmIHttpClinetListener(this);
        carRequest.doRequest();
    }

    @Override
    public void httpSuccessListener(Bundle successMsg) {
        super.httpSuccessListener(successMsg);
        pull_ly.setRefreshing(false);
        pull_ly.setLoading(false);
        if(mBaseResult==null){return;}
        if(carRequest!=null&&action.equalsIgnoreCase(carRequest.getAction())){
            if(mBaseResult.getStatus().getRetCode()==0){
                mList = mBaseResult.toArray(CarBean.class,"list");
                if(BaseTools.isValueEmpty(mList)){
                    ero_ll.setVisibility(View.VISIBLE);
                  mListView.setVisibility(View.GONE);
                    setRight_text("");
                }else{
                    setRight_text("管理");
                    mAdapter.setDatas(mList);
                    mListView.setVisibility(View.VISIBLE);
                    ero_ll.setVisibility(View.GONE);
                }
            }
            toast(mBaseResult.getStatus().getMsg());
        }else if(delRequest!=null&&action.equalsIgnoreCase(delRequest.getAction())){
            if(mBaseResult.getStatus().getRetCode()==0){
                mList.remove(position);
                isDelete=false;
                setRight_text("管理");
                if(BaseTools.isValueEmpty(mList)){
                    ero_ll.setVisibility(View.VISIBLE);
                    setRight_text("");
                    mListView.setVisibility(View.GONE);
                    mAdapter.setDatas(mList,false);
                }else{
                    mAdapter.setDatas(mList,false);
                    ero_ll.setVisibility(View.GONE);
                    mListView.setVisibility(View.VISIBLE);
                }
            }
            toast(mBaseResult.getStatus().getMsg());
        }
    }
    public void toDelete(View view){
         position = (int) view.getTag();
        delRequest = new HttpCarRequest(this,2);
        delRequest.delCarNum(mList.get(position).getId());
        delRequest.setmIHttpClinetListener(this);
        delRequest.doRequest();
    }
    public void addCar(View view){
        isDelete = false;
        setRight_text("管理");
        mAdapter.notifyDataSetChanged();
        showActivity(AddCarActivity.class,null, Constrants.ADD_CAR);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==Constrants.ADD_CAR&&resultCode==Constrants.ADD_CAR){
            getData();
        }
    }
}
